﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class Trap : Character
    {
#region Properties
        protected int _timeLive;
        public int TimeLive
        {
            get { return _timeLive; }
            set { _timeLive = value; }
        }

        protected int _countTimeLive;
        public int CountTimeLive
        {
            get { return _countTimeLive; }
            set { _countTimeLive = value; }
        }
#endregion

#region Methods
        public virtual bool CheckAttackWith(Character characterIsAttacked, GameTime gametime)
        {
            bool attack = false;
            if (_state != State.Disappear)
            {
                if (this.IsCollisionWith(characterIsAttacked))
                {
                    attack = true;
                    double gt = gametime.TotalGameTime.TotalMilliseconds;
                    EffectOnZombie(characterIsAttacked);
                }
            }
            return attack;
        }

        public virtual void EffectOnZombie(Character characterIsAttacked)
        {
        }
#endregion

#region Initiation
        public Trap(List<Texture2D> listTextures,
            Vector2 posotion, 
            int damage,
            int timeLive,
            float delay,
            int price,
            float scaleTexture): base(listTextures, posotion, 0, damage, 0, delay, scaleTexture, price, null)
        {
            _listTextures = listTextures;
            _position = posotion;
            _damage = damage;
            _timeLive = timeLive; _countTimeLive = 0;
            _delay = delay;
            _price = price;
            _scaleTexture = scaleTexture;
            _scaleRect = -20;
            _rectangle = new Rectangle
                ((int)_position.X, (int)_position.Y, _listTextures[0].Width - 10, _listTextures[0].Height - 10);
            _indexTexture = 0;
            _state = State.Move;
        }
#endregion

#region Update & Draw
        public override void Update(GameTime gametime, Vector2 position)
        {
            if (_state != State.Disappear)
            {
                double gt = gametime.TotalGameTime.Milliseconds;
                if ((gt % NormalDelay) < _delay)
                {                    
                    _indexTexture++;
                    if (_indexTexture >= _listTextures.Count)
                    {
                        _indexTexture = 0;
                        _countTimeLive++;
                    }
                    if (_countTimeLive >= _timeLive)
                        _state = State.Disappear;
                }
            }            
        }

        public override void Draw(SpriteBatch spritebatch, GameTime gametime)
        {
            if (_state != State.Disappear)
                spritebatch.Draw(_listTextures[_indexTexture], _position, null, _colorTexture, 0, Vector2.Zero, _scaleTexture, SpriteEffects.None, 0);
        }
#endregion
    }
}
